package com.yc.cloud.portal.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.openai.dto.ImageMJWebhookDto;
import com.yc.cloud.openai.dto.request.ImageMJProcessParam;
import com.yc.cloud.openai.dto.request.ImageMJGenerateParam;
import com.yc.cloud.openai.service.ImageAiGCService;
import com.yc.cloud.openai.vo.ImageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("ai/image")
@Slf4j
@Api(tags = "AI-图像模块")
public class ImageAiGCController {

    private ImageAiGCService imageAiGCService;

    public ImageAiGCController(ImageAiGCService imageAiGCService) {
        this.imageAiGCService = imageAiGCService;
    }

    @PostMapping("/mj/generate")
    @ApiOperation(value = "MJ文生图-异步方式", notes = "通过文字描述生成图片,该接口耗时比较多,采用webhook方式回调结果")
    public ResponseResult<String> generateImageByMj(@Validated @RequestBody ImageMJGenerateParam param) {
        val taskId = imageAiGCService.generate(param.getPrompt(), param.getWebhookUrl());
        return StrUtil.isNotEmpty(taskId) ? ResponseResult.success(taskId) : ResponseResult.failed();
    }

    @PostMapping("/mj/process")
    @ApiOperation(value = "MJ-处理图片", notes = "选择图片集中某张图片进行放大操作")
    public ResponseResult<ImageVo> processImageByMj(@Validated @RequestBody ImageMJProcessParam param) {
        val result = imageAiGCService.process(param);
        return result!=null ? ResponseResult.success(result) : ResponseResult.failed();
    }


    @PostMapping("/mj/generate/webhook")
    @ApiIgnore
    @ApiOperation(value = "MJ文生图回调", notes = "通过文字描述生成图片")
    public void generateImageByMjWebhook(@RequestBody ImageMJWebhookDto webhookDto) {
        log.info("MJ文生图回调：{}", JSONUtil.toJsonPrettyStr(webhookDto));
    }
}
